/**
 * Copyright 2012 Simple-Jdbc-Lightweight

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package org.mlu.jdbc.simple;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.mlu.jdbc.simple.utils.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mlungisi Sincuba
 * @since 1.0
 */
public class DataSourceDetails {
    
    private static final Logger logger = LoggerFactory.getLogger(DataSourceDetails.class);
    private static DataSource dataSource;
    private static final DataSourceDetails dataSourceDetails = new DataSourceDetails();

    private DataSourceDetails() {
    }
    
    public static DataSourceDetails getInstance() {
        return dataSourceDetails;
    }
    
    public static DataSource getDataSource() throws FileNotFoundException{
        if (dataSource == null) {
            logger.debug("Finding DataSource from project path on db.properties");
            Properties p = PropertiesUtils.getPropertiesFile("db.properties");
            String dataSourceName = "DefailtDS";
            String driver = p.getProperty("driver");
            String url = p.getProperty("url");
            String user = p.getProperty("user");
            String pwd = p.getProperty("pwd");
            int minPool = Integer.parseInt(p.getProperty("minPool", "1"));
            int maxPool = Integer.parseInt(p.getProperty("maxPool", "15"));
            int maxSize = Integer.parseInt(p.getProperty("maxSize", "30"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSource(String fileName) throws FileNotFoundException{
        if (dataSource == null) {
            Properties p = PropertiesUtils.getPropertiesFile(fileName);
            String dataSourceName = "DefailtDS";
            String driver = p.getProperty("driver");
            String url = p.getProperty("url");
            String user = p.getProperty("user");
            String pwd = p.getProperty("pwd");
            int minPool = Integer.parseInt(p.getProperty("minPool", "1"));
            int maxPool = Integer.parseInt(p.getProperty("maxPool", "15"));
            int maxSize = Integer.parseInt(p.getProperty("maxSize", "30"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSourceForWeb(InputStream is) {
        if (dataSource == null) {
            logger.debug("Getting DataSource from is: {}" + is);
            Properties p = PropertiesUtils.getPropertiesFile(is);
            String dataSourceName = "DefailtDS";
            String driver = p.getProperty("driver");
            String url = p.getProperty("url");
            String user = p.getProperty("user");
            String pwd = p.getProperty("pwd");
            int minPool = Integer.parseInt(p.getProperty("minPool", "1"));
            int maxPool = Integer.parseInt(p.getProperty("maxPool", "15"));
            int maxSize = Integer.parseInt(p.getProperty("maxSize", "30"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSourceFromClassPath() {
        if (dataSource == null) {
            logger.debug("Getting DataSource from class path for filename db.properties");
            ResourceBundle rb = ResourceBundle.getBundle("db");
            String dataSourceName = "DefaultDS";
            String driver = rb.getString("driver") ;
            String url = rb.getString("url");
            String user = rb.getString("user");
            String pwd = rb.getString("pwd");
            int minPool = Integer.parseInt(rb.getString("minPool"));
            int maxPool = Integer.parseInt(rb.getString("maxPool"));
            int maxSize = Integer.parseInt(rb.getString("maxSize"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSourceFromClassPath(String fileNameWithoutExtention) {
        if (dataSource == null) {
            logger.debug("Getting DataSource from class path for filename db.properties");
            ResourceBundle rb = ResourceBundle.getBundle(fileNameWithoutExtention);
            String dataSourceName = "DefaultDS";
            String driver = rb.getString("driver") ;
            String url = rb.getString("url");
            String user = rb.getString("user");
            String pwd = rb.getString("pwd");
            int minPool = Integer.parseInt(rb.getString("minPool"));
            int maxPool = Integer.parseInt(rb.getString("maxPool"));
            int maxSize = Integer.parseInt(rb.getString("maxSize"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
}
